package com.liuhouse.utils;

import com.liuhouse.vo.MenuTreeVo;
import com.liuhouse.vo.MenuVo;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

//新增角色- 获取菜单下拉树列表
public class SystemConverter {

    //无参构造器
    private SystemConverter(){}

    //将从数据库查询出来的菜单构建出树形结构
    public static List<MenuTreeVo> buildMenuSelectTree(List<MenuVo> menus){
        //将menu转化为menuTreeVos对象
        List<MenuTreeVo> menuTreeVos = menus.stream()
                .map(menu -> new MenuTreeVo(menu.getId(), menu.getMenuName(), menu.getParentId(), null))
                .collect(Collectors.toList());
        List<MenuTreeVo> treeVos = menuTreeVos.stream()
                .filter(menuTreeVo -> menuTreeVo.getParentId().equals(0L))
                .map(menuTreeVo -> menuTreeVo.setChildren(getChildList(menuTreeVos, menuTreeVo)))
                .collect(Collectors.toList());
        return treeVos;
    }


    //得到子节点列表
    private static List<MenuTreeVo> getChildList(List<MenuTreeVo> menuTreeVos, MenuTreeVo option) {
        List<MenuTreeVo> collect = menuTreeVos.stream()
                .filter(o -> Objects.equals(o.getParentId(), option.getId()))
                .map(o -> o.setChildren(getChildList(menuTreeVos, o)))
                .collect(Collectors.toList());
        return collect;
    }


}
